#!/usr/bin/env python
import roslib; 

roslib.load_manifest('dialog_manager')
roslib.load_manifest('julius')
roslib.load_manifest('festival')

import rospy
from julius.msg import Sentence
from festival.msg import Speech
from dialog_manager.srv import *

ears_enabled = True

def handle_toggle_ears(req):
  global ears_enabled
  ears_enabled = req.state
  rospy.loginfo("Ears State: %d", req.state)
  return ToggleEarsResponse(True)

def callback_recognized_speech(data):
  if ears_enabled == True:
    sentence = data.text.lstrip("<s> ").rstrip(" </s> ")
    words = data.words[1:len(data.words) - 1]
    rospy.loginfo(sentence)
    pub = rospy.Publisher('speech', Speech)
    pub.publish(Speech(words[0] + " there."))

def personality():
  rospy.init_node('processor', anonymous=True)
  rospy.Service('toggle_ears', ToggleEars, handle_toggle_ears)
  rospy.Subscriber("recognized_speech", Sentence, callback_recognized_speech)
  print "SURG Personality is active"
  rospy.spin()

if __name__ == '__main__':
  personality()
